/**
 * 产品修改js
 */
var comValidform = null;
var returnStatus = false;
var productInfoUpdateStatus = {
	dataCache : null,
	key:null,
	proStatus:null,
	hotSale:null,
	saleOut:null,
	sequence:null,

	startEvent : function() {
		productInfoUpdateStatus.showInfo();

		//只有当设置为上架时才能设置热销
		$('select[name="proStatus"]').change(function() {
			var proStatus = $(this).val();
			productInfoUpdateStatus.proStatus = proStatus;
			if ( proStatus == "1") {
				$('#txTr').css('display', '');
			} else {
				$('#txTr').css('display', 'none');
				$('select[name="hotSale"]').val(0);
			}
		});
		
		//只有当设置为上架时才能设置热销
		$('select[name="hotSale"]').change(function() {
			var hotSale = $(this).val();
			productInfoUpdateStatus.hotSale = hotSale;
		});
		
		//只有当设置为上架时才能设置热销
		$('select[name="sequence"]').change(function() {
			var sequence = $(this).val();
			productInfoUpdateStatus.sequence = sequence;
		});
		
		//只有当设置为上架时才能设置热销
		$('select[name="saleOut"]').change(function() {
			var saleOut = $(this).val();
			productInfoUpdateStatus.saleOut = saleOut;
		});


		comValidform = $(".com-addForm").Validform({
			datatype : {
				"m0-2" : /^[0-9]+(.[0-9]{1,2})?$/
			},
			tiptype : 2,
			tipSweep : true,
			beforeSubmit : function(curform) {
				var params = jsUtils.serializeJson($('.com-addForm'));
				if(params.topSaleStr != undefined || params.channel != null){
					params.topSaleStr = params.topSaleStr.toString();
				}

				var url = 'mgr/ProductInfoCtrl/updateStatus';

				var cb = function(data) {
					alert(data.msg);
					// 成功返回
					if (data.resultCode == 200) {
						localStorage.removeItem(productInfoUpdateStatus.key);
						
						var singleObj = data.result;

						localStorage.setItem(singleObj.id
								+ "_ProductType02_Key", JSON
								.stringify(singleObj));
						// 关闭窗口并刷新数据
						art.dialog.close();
						returnStatus = true;
					}
				};
				sendApiNoLoadSync(url, params, cb, 'POST');
				return false;
			}
		});
	},

	showInfo: function() {
	 
		var id = art.dialog.data('updId');
		productInfoUpdateStatus.key = id + "_ProductType02_Key";
		var ret = eval('(' + localStorage.getItem(productInfoUpdateStatus.key) + ')');
		$('input[name="name"]').val(ret.name);
		$('input[name="id"]').val(ret.id);
		$('select[name="hotSale"]').val(ret.hotSale);
		$('select[name="proStatus"]').val(ret.proStatus);
		$('select[name="sequence"]').val(ret.sequence);
		$('select[name="saleOut"]').val(ret.saleOut);

		$('select[name="waitSale"]').val(ret.waitSale);
		if(ret.topSaleStr != null){
			if(ret.topSaleStr.indexOf("1") >= 0){
				$("input:checkbox[value='1']").attr('checked','true');
			}

			if(ret.topSaleStr.indexOf("2") >= 0){
				$("input:checkbox[value='2']").attr('checked','true');
			}

			if(ret.topSaleStr.indexOf("3") >= 0){
				$("input:checkbox[value='3']").attr('checked','true');
			}
		}


		productInfoUpdateStatus.proStatus = ret.proStatus;
		productInfoUpdateStatus.hotSale = ret.hotSale;
		productInfoUpdateStatus.sequence = ret.sequence;
		productInfoUpdateStatus.saleOut = ret.saleOut;
	},
	init : function() {
		productInfoUpdateStatus.startEvent();
	}
}
$(document).ready(function() {
	productInfoUpdateStatus.init();
});
function submitForm() {
	comValidform.submitForm();
	return returnStatus;
}